<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>开空运主单</title>
        <% include("/common/common_css_js.html"){} %>
    </head>
    <body>
        <div style="width: 1000px;margin: 5px 0;margin-left: 5px;">
            <% if(so.hasPermission("bMasterBillFlight:save")){%>
                <a id="saveBtn" name="saveBtn" class="mini-button" iconCls="icon-save" onclick="saveMasterBill()">保存</a>
            <% } %>
        </div>

        <div id="masterBillForm" style="padding-bottom: 5px;" class="mini-fit">
            <div>
                注意：
                <div style="color: red;margin-left: 5px;font-weight: bold">1.【承运方式】为外发不会检测主单号，请务必自行保证外发主单单号准确无误</div>
                <div style="color: red;margin-left: 5px;font-weight: bold">2.【承运方式】为外发请手动填写相关各项费用</div>
            </div>
            <span>承运方式:</span><input id="transportWayId" name="transportWayId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                     required="true" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择承运方式" onvaluechanged="transportWayIdChange"/>

            <span id="outUnitTitle">外发单位:</span>
            <div id="outUnitId" name="outUnitId"
                 class="mini-combobox"
                 style="width:250px;"
                 popupWidth="600" textField="name" valueField="id"
                 allowInput="true" required="true" requiredErrorText="外发单位不能为空"
                 emptyText="外发单位名称">
                <div property="columns">
                    <div header="ID" field="id" width="30"></div>
                    <div header="单位名称" field="name"></div>
                    <div header="联系人" field="contact"></div>
                    <div header="电话" field="phone"></div>
                    <div header="地址" field="address"></div>
                </div>
            </div>

            <div style="margin-top: 5px"></div>
            <span>航司两字码:</span>
            <div id="airlinesTwoCode" name="airlinesTwoCode"
                 class="mini-combobox"
                 style="width:70px;"
                 popupWidth="400" textField="twoCode" valueField="id"
                 allowInput="true" required="true" requiredErrorText="航空公司两字码不能为空"
                 emptyText="两字码"
                 onvaluechanged="airlinesTwoCodeChange">
                <div property="columns">
                    <div header="ID" field="id" width="30"></div>
                    <div header="两字码" field="twoCode"></div>
                    <div header="简称" field="shortName"></div>
                </div>
            </div>

            <span>主单号:</span><input id="id" name="id" emptyText="请输入主单号"  class="mini-textbox" selectOnFocus="true" required="true" requiredErrorText="主单号不能为空" onvaluechanged="idChange"/>

            <span>配货方式:</span><input id="loadWayId" name="loadWayId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                     required="true" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择配货方式" onvaluechanged="loadWayIdChange"/>

            <span>货物类型:</span><input id="goodsTypeId" name="goodsTypeId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                     required="true" allowInput="false" showNullItem="false" nullItemText="请选择..." requiredErrorText="没有选择货物类型"/>

            <span>目的市:</span><input id="destCityId" name="destCityId" class="mini-treeselect"
                                    multiSelect="false" valueField="id" textField="name" parentField="pid" allowInput="true" valueFromSelect="true" virtualScroll="true"
                                    style="width: 220px;" expandOnLoad="0" required="true" nullItemText="请选择..." onvaluechanged="destCityIdChange"/>

            <div style="margin-top: 5px"></div>
            <span id="startAirportSpan">始发机场:</span>
            <div id="airportId" name="airportId" class="mini-combobox" style="width:150px;"  popupWidth="1000" textField="name" valueField="id"
                 required="true" requiredErrorText="始发机场不能为空"
                 allowInput="true"
                 emptyText="始发机场名称"
                 onvaluechanged="airportIdChange">
                <div property="columns">
<!--                    <div header="ID" field="id" width="30"></div>-->
                    <div header="机场名称" field="name"></div>
                    <div header="简称" field="shortName"></div>
                </div>
            </div>

            <span>航空公司:</span>
            <div id="airlinesId" name="airlinesId"
                 class="mini-combobox"
                 style="width:250px;"
                 popupWidth="600" textField="name" valueField="id"
                 allowInput="true" required="true" requiredErrorText="航空公司不能为空"
                 onvaluechanged="flightDateChanged"
                 emptyText="请选择航空公司">
                <div property="columns">
<!--                    <div header="ID" field="id" width="30"></div>-->
                    <div header="名称" field="name"></div>
                    <div header="简称" field="shortName"></div>
                    <div header="两字码" field="twoCode"></div>
                </div>
            </div>

            <span>航班日期:</span>
            <input id="flightDate" name="flightDate" class="mini-datepicker" style="width:150px;"  nullValue="null" required="true" requiredErrorText="航班日期不能为空"
                   format="yyyy-MM-dd"  showOkButton="true" showClearButton="true" onvaluechanged="flightDateChanged"/>

            <span>航班号:</span>
            <div id="flightNo" name="flightNo" class="mini-combobox" style="width:150px;"  popupWidth="600" textField="name" valueField="id"
                 required="true" requiredErrorText="航班号不能为空"
                 multiSelect="false" oncloseclick="flightOnCloseClick" onvaluechanged="flightNoChanged">
                <div property="columns">
                    <div header="ID" field="id"></div>
                    <div header="航班号" field="name"></div>
                    <div header="起飞时间" field="planTakeOffTime"></div>
                    <div header="航空公司简称" field="shortName"></div>
                </div>
            </div>
            <!-- 隐藏字段 -->
            <input id="flightName" name="flightName" class="mini-hidden" emptyText="航班名称" required="true"   requiredErrorText="航班名称不能为空"/>

            <span>计划出发时间:</span>
            <input id="planTakeOffTime" name="planTakeOffTime" class="mini-textbox" emptyText="计划出发时间" style="width:100px;" selectOnFocus="true" required="true" enabled="false" requiredErrorText="计划出发时间不能为空"/>

            <div style="margin-top: 5px"></div>

            <div style="display: flex;justify-content:space-between;align-items:center;width: 1400px;">
                <div style="width:60px;">分单列表:</div>
                <div style="width:1340px;padding:0px;">
                    <div class="mini-toolbar" style="width:100%;border-bottom:0;padding:2px;">
                        <span>分单号:</span>
                        <div id="billId" name="billId" class="mini-autocomplete" style="width:150px;"  popupWidth="1200" textField="id" valueField="id"
                             requiredErrorText="分单号不能为空"
                             multiSelect=false
                             searchField="key"
                             url="${base}/pages/native_bill_manage/getBNativeBillPojoByCnd?transportSpeciesId=71&pullDisposeId=232"
                             allowInput="true"
                             emptyText="输入分单号"
                             onvaluechanged="billIdChanged"
                             onfocus="billIdFocus()"
                            >
                            <div property="columns">
                                <div header="分单号" field="id" width="60"></div>
                                <div header="运输种类" field="transportSpeciesId" data-options='{cacheName:"sysDic"}' renderer="commonRender"></div>
                                <div header="物品名称" field="goodsName"></div>
                                <div header="包装" field="packingWayId" data-options='{cacheName:"sysDic"}' renderer="commonRender"></div>
                                <div header="剩余件数" field="totalNumber"></div>
                                <div header="剩余实际重量" field="actualWeight" renderer="public_moneyRenderer"></div>
                                <div header="剩余计费重量" field="chargeableWeight" renderer="public_moneyRenderer"></div>
                                <div header="重量费率" field="weightRate" renderer="public_moneyRenderer"></div>
                                <div header="剩余体积" field="volume" renderer="public_moneyRenderer"></div>
                                <div header="体积费率" field="volumeRate" renderer="public_moneyRenderer"></div>
                                <div header="航空公司" field="airlinesId" data-options='{cacheName:"airlines"}' renderer="commonRender"></div>
                                <div header="航班名称" field="flightName"></div>
                                <div header="航班日期" field="flightDate" dateFormat="yyyy-MM-dd" renderer="ondayRenderer"></div>
                                <div header="开单人" field="createUserId" data-options='{cacheName:"sysUser"}' renderer="commonRender"></div>
                            </div>
                        </div>


                        <span class="separator"></span>
                        <a class="mini-button" iconCls="icon-remove" onclick="removeSelectedRow()"  tooltip="剔除...">剔除</a>
                        <span class="separator"></span>
                        <div id="ck1" name="ck1" class="mini-checkbox" readOnly="false" text="开放约束" value="false" enabled="false" onvaluechanged="ck1Changed"></div>
                    </div>
                    <div id="selectedBillDatagrid" class="mini-datagrid" style="width:100%;height:200px;"
                         idField="id"
                         showPager="false"
                         allowCellEdit="true" allowCellSelect="true" multiSelect="true"
                         showGroupsSummary="true" showSummaryRow="true" summaryPosition="bottom"
                         editNextOnEnterKey="true"  editNextRowCell="true"
                         oncellendedit="gridCellEndEdit"
                         ondrawsummarycell="summaryRight"
                    >
                        <div property="columns">
                            <div type="checkcolumn"></div>
                            <div field="id" headerAlign="center" align="center"  width="50" >分单号</div>
                            <div field="goodsName" width="80" headerAlign="center" align="left" >物品名称</div>
                            <div field="packingWayId" width="40" headerAlign="center" align="center" data-options='{cacheName:"sysDic"}' renderer="commonRender">包装</div>
                            <div field="totalNumber" width="40" headerAlign="center" align="right" summaryType="sum">配载件数
                                <input property="editor" class="mini-textbox"/>
                            </div>
                            <div field="actualWeight" width="50" headerAlign="center" align="right" summaryType="sum" renderer="public_moneyRenderer">配载实际重量
                                <input property="editor" class="mini-textbox"/>
                            </div>
                            <div field="chargeableWeight" width="50" headerAlign="center" align="right" summaryType="sum" renderer="public_moneyRenderer">配载计费重量
                                <input property="editor" class="mini-textbox"/>
                            </div>
                            <div field="weightRate" width="40" headerAlign="center" align="right" renderer="public_moneyRenderer">重量费率</div>
                            <div field="volume" width="40" headerAlign="center" align="right" summaryType="sum" renderer="public_moneyRenderer">配载体积
                                <input property="editor" class="mini-textbox"/>
                            </div>
                            <div field="volumeRate" width="40" headerAlign="center" align="right" renderer="public_moneyRenderer">体积费率</div>
                            <div field="airlinesId" width="150" headerAlign="center" align="left" data-options='{cacheName:"airlines"}' renderer="commonRender">航空公司</div>
                            <div field="flightName" width="50" headerAlign="center" align="center">航班名称</div>
                            <div field="flightDate" width="50" headerAlign="center" align="center" dateFormat="yyyy-MM-dd" renderer="ondayRenderer" >航班日期</div>
                            <div field="createUserId" width="50" headerAlign="center" data-options='{cacheName:"sysUser"}' renderer="commonRender">开单人</div>
                        </div>
                    </div>
                </div>
            </div>

            <div style="margin-top: 5px"></div>
            <span>发货人:</span>
            <input id="agentCompanyId" name="agentCompanyId" class="mini-combobox" style="width:400px;" textField="name" valueField="id" emptyText="请选择..."
                                   required="true" allowInput="false" showNullItem="false" nullItemText="请选择..."  requiredErrorText="没有选择发货人" onvaluechanged="agentCompanyIdChange" />
            <input id="agentCompanyName" name="agentCompanyName"  emptyText="发货人名称"  class="mini-hidden" required="true" requiredErrorText="发货人名称不能为空" />
            <input id="agentCompanyPhone" name="agentCompanyPhone" emptyText="发货人电话" style="width:400px;"  class="mini-textbox" required="true" requiredErrorText="发货人电话不能为空" />
            <input id="agentCompanyAddress" name="agentCompanyAddress" emptyText="发货人地址" style="width:400px;"  class="mini-textbox" required="true" requiredErrorText="发货人地址不能为空" />

            <div style="margin-top: 5px"></div>
            <span>收货人:</span>
            <input id="receiveCustomerId" name="receiveCustomerId" class="mini-combobox" style="width:400px;" textField="name" valueField="id" emptyText="请选择..."
                   required="true" allowInput="false" showNullItem="true" nullItemText="请选择..."  requiredErrorText="没有选择收货人" onvaluechanged="receiveCustomerIdChanged" />
            <input id="receiveCustomerName" name="receiveCustomerName" emptyText="收货人名称"  class="mini-hidden" required="true" requiredErrorText="收货人名称不能为空" />
            <input id="receiveCustomerPhone" name="receiveCustomerPhone" emptyText="收货人电话" style="width:400px;"  class="mini-textbox" required="true" requiredErrorText="收货人电话不能为空" />
            <input id="receiveCustomerAddress" name="receiveCustomerAddress" emptyText="收货人地址" style="width:400px;"  class="mini-textbox" required="true" requiredErrorText="收货人地址不能为空" />

            <div style="margin-top: 5px"></div>
            <span>运价种类:</span><input id="freightSpeciesId" name="freightSpeciesId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                   required="true" allowInput="false" showNullItem="false" nullItemText="请选择..."  requiredErrorText="没有选择运价种类" />

            <span>商品代号:</span><input id="goodsCodeId" name="goodsCodeId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                   required="true" allowInput="false" showNullItem="false" nullItemText="请选择..."  requiredErrorText="没有选择商品代号" />

            <span>货物名称:</span><input id="goodsName" name="goodsName" emptyText="货物名称"  class="mini-textbox" selectOnFocus="true" required="true" requiredErrorText="货物名称不能为空"/>

            <span>包装方式:</span><input id="packingWayId" name="packingWayId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                     required="true" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择包装方式"/>

            <div style="margin-top: 5px"></div>
            <span>总件数:</span><input id="totalNumber" name="totalNumber" vtype="int" value="0" emptyText="总件数"  class="mini-textbox" style="width: 100px" required="true"  requiredErrorText="总件数不能为空" />
            <span>实际重量:</span><input id="actualWeight" name="actualWeight" vtype="float" value="0" emptyText="实际重量"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="实际重量不能为空" onvaluechanged="actualWeightChanged"/>
            <span>计费重量:</span><input id="chargeableWeight" name="chargeableWeight" vtype="float" value="0" emptyText="计费重量"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="计费重量不能为空" onvaluechanged="chargeableWeightChanged"/>
            <span>重量费率:</span><input id="weightRate" name="weightRate" vtype="float" value="0" emptyText="重量费率"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="重量费率不能为空" onvaluechanged="weightRateChanged"/>
            <span>航空运费:</span><input id="transportCost" name="transportCost" vtype="float" value="0" emptyText="航空运费"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="航空运费不能为空"/>

            <div style="margin-top: 5px"></div>
            <span>货站计重分类:</span><input id="calcWeightCategoryId" name="calcWeightCategoryId" class="mini-combobox" style="width:100px;" textField="name" valueField="id" emptyText="请选择..."
                                   required="true" allowInput="false" showNullItem="false" nullItemText="请选择..."  requiredErrorText="没有选择计算重量分类" onvaluechanged="calcWeightCategoryIdChange"/>
            <span>货站:</span><input id="airportGoodsSiteId" name="airportGoodsSiteId" class="mini-combobox" style="width:300px;" textField="name" valueField="id" emptyText="请选择..."
                                     required="true" allowInput="false" showNullItem="true" nullItemText="请选择..."  requiredErrorText="没有选择货站" onvaluechanged="airportGoodsSiteIdChange" />
            <div style="margin-top: 5px"></div>
            <span>声明价值附加费:</span><input id="declaredValue" name="declaredValue" vtype="float" value="0" emptyText="声明价值附加费"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="声明价值附加费不能为空"/>
            <span>地面运费:</span><input id="groundAmount" name="groundAmount" vtype="float" value="0" emptyText="地面运费"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="地面运费不能为空"/>
            <span>印花税:</span><input id="stampDuty" name="stampDuty" vtype="float" value="0" emptyText="印花税"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="印花税不能为空"/>
            <span>保险费:</span><input id="insuranceFee" name="insuranceFee" vtype="float" value="0" emptyText="保险费"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="保险费不能为空"/>
            <span>航空附加费:</span><input id="airlinesAddCost" name="airlinesAddCost" vtype="float" value="0" emptyText="航空附加费"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="航空附加费不能为空"/>
            <a id="calcAirlinesAddCostBtn" class="mini-button"  onclick="calcAirlinesAddCost()" tooltip="计算航空附加费...">计算航空附加费</a>

            <div style="margin-top: 5px"></div>
            <div style="display: flex;justify-content: flex-start;width: 1200px;">
                <div style="margin-top: 50px;">
                    <span>其他费用:</span>
                </div>
                <div style="width: 5px"></div>
                <div style="width:600px;border-bottom:0;padding:0px;">
                    <div class="mini-toolbar" style="width:600px;border-bottom:0;padding:0px;">
                        <a class="mini-button" iconCls="icon-add" onclick="addOtherChargesRow()" tooltip="增加...">增加</a>
                        <span class="separator"></span>
                        <a class="mini-button" iconCls="icon-remove" onclick="removeOtherChargesRow()" tooltip="删除...">删除</a>
                        <span class="separator"></span>
                        <a class="mini-button" iconCls="icon-ok" onclick="calcOtherCharges()" tooltip="计算其他费用...">计算其他费用</a>
                    </div>
                    <div id="otherChargeDatagrid" class="mini-datagrid" style="width:600px;height:100px;"
                         idField="id"
                         showPager="false"
                         allowCellEdit="true" allowCellSelect="true" multiSelect="true"
                         editNextOnEnterKey="true"  editNextRowCell="true" oncelleditenter="otherChargeDatagridEnter">
                        <div property="columns">
                            <div type="checkcolumn"></div>
                            <div type="indexcolumn">序号</div>
                            <div name="name"  field="name" headerAlign="center" allowSort="true" width="150" >其他费用名目
                                <input property="editor" class="mini-textbox" style="width:100%;" minWidth="200" />
                            </div>
                            <div name ="amount" field="amount" width="100" allowSort="true" >金额
                                <input property="editor" class="mini-textbox"  value="0" style="width:100%;"/>
                            </div>
                            <div field="comment" width="120" headerAlign="center" allowSort="true">备注
                                <input property="editor" class="mini-textarea" style="width:200px;" minWidth="200" minHeight="50"/>
                            </div>
                        </div>
                    </div>
                </div>
                <div style="width: 5px"></div>
                <div style="display: flex;flex-direction: column">
                    <div style="display: flex;align-items: center">
                        <span>金额:</span>
                        <input id="otherCharges" name="otherCharges" value="0" emptyText="其他费用"  class="mini-textbox" required="true" requiredErrorText="其他费用说明不能为空" enabled="false"/>
                    </div>
                    <div style="margin-top: 5px"></div>
                    <div style="display: flex;align-items: center">
                        <span>说明:</span>
                        <textarea id="otherChargesComment" name="otherChargesComment" class="mini-textarea" selectOnFocus="true" style="width: 400px;height: 96px" emptyText="输入其他费用说明" required="false"></textarea>
                    </div>


                </div>
            </div>


            <div style="margin-top: 5px"></div>
            <span>付款方式:</span><input id="masterBillPaymentMethodId" name="masterBillPaymentMethodId" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                     required="true" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择付款方式"  onvaluechanged="masterBillPaymentMethodIdChange" />
            <span>总金额:</span><input id="totalAmount" name="totalAmount" vtype="float" value="0" emptyText="总金额"  class="mini-textbox" style="width: 100px" required="true" requiredErrorText="总金额不能为空"/>

            <span>开单日期:</span><input id="openMasterBillDate" name="openMasterBillDate" class="mini-datepicker" style="width:200px;" value="${openMasterBillDate!}"  nullValue="null" format="yyyy-MM-dd"  showOkButton="true" showClearButton="true"/>

            <span>开单部门:</span><input id="openMasterBillDepartmentId" name="openMasterBillDepartmentId" value="${openMasterBillDepartmentId!}" class="mini-treeselect" style="width:250px;" textField="name" valueField="id" emptyText="请选择..."
                                     required="true" allowInput="false" showNullItem="true" expandOnLoad="true" nullItemText="请选择..." requiredErrorText="没有选择开单部门"  />

            <span>开单人:</span><input id="openMasterBillUserId" name="openMasterBillUserId" value="${openMasterBillUserId!}" class="mini-combobox" style="width:150px;" textField="name" valueField="id" emptyText="请选择..."
                                    required="true" allowInput="false" showNullItem="true" nullItemText="请选择..." requiredErrorText="没有选择开单人" />

            <div style="margin-top: 5px"></div>
            <span>拉货调整金额:</span><input id="dropAmount" name="dropAmount"  vtype="float" value="0.0" emptyText="拉货累计金额"  class="mini-textbox" style="width: 100px" required="true" enabled="false" />
            <span>结算金额:</span><input id="settlementAmount" name="settlementAmount"  vtype="float" value="0.0" emptyText="结算金额"  class="mini-textbox" style="width: 100px" required="true" enabled="false" />
            <span style="color: red">结算金额在保存时会自动更新，页面显示不会做自动变化</span>



            <div style="margin-top: 5px"></div>
            <span>备注:</span><textarea id="comment" name="comment" class="mini-textarea" style="width: 80%;height: 50px" emptyText="请输入物品信息"  required="false"></textarea>

            <!-- 默认值隐藏区域  -->
            <input id="transportCostStatusId" name="transportCostStatusId" value="321" emptyText="运输成本结算状态ID"  class="mini-hidden" required="true" enabled="false" />
            <input id="groundAmountStatusId" name="groundAmountStatusId" value="321" emptyText="地面运费结算状态ID"  class="mini-hidden" required="true" enabled="false" />
            <input id="airlinesAddCostStatusId" name="airlinesAddCostStatusId" value="321" emptyText="航空附加结算状态ID"  class="mini-hidden" required="true" enabled="false" />
            <input id="otherChargesStatusId" name="otherChargesStatusId" value="321" emptyText="其他费用结算状态ID"  class="mini-hidden" required="true" enabled="false" />

            <input id="dropStatusId" name="dropStatusId" value="361" emptyText="拉货状态ID"  class="mini-hidden" style="width: 100px" required="true" enabled="false" />
            <input id="accountCheckStatusId" name="accountCheckStatusId" value="431" emptyText="对账状态ID"  class="mini-hidden"  required="true" enabled="false" />
            <input id="settlementStatusId" name="settlementStatusId" value="321" emptyText="结算状态ID"  class="mini-hidden"  required="true" enabled="false" />

            <input id="loadNativeBillIds" name="loadNativeBillIds" value=""  class="mini-hidden"  required="false" enabled="false" />

            <!-- 实际配载总重量,用于成本分摊计算 -->
            <input id="actualLoadTotalWeight" name="actualLoadTotalWeight" value="0"  class="mini-hidden"  required="false" enabled="false" />

            <!-- 拉货调整金额隐藏区域  -->
            <input id="pullDisposeTransportCostAmount" name="pullDisposeTransportCostAmount"  vtype="float" value="0.0" emptyText="航空运费调整金额"  class="mini-hidden" required="true" enabled="false" />
            <input id="pullDisposeGroundAmount" name="pullDisposeGroundAmount"  vtype="float" value="0.0" emptyText="地面运费调整金额"  class="mini-hidden" required="true" enabled="false" />
            <input id="pullDisposeAirlinesAddCostAmount" name="pullDisposeAirlinesAddCostAmount"  vtype="float" value="0.0" emptyText="航空附加费用调整金额"  class="mini-hidden" required="true" enabled="false" />
            <input id="pullDisposeOtherChargesAmount" name="pullDisposeOtherChargesAmount"  vtype="float" value="0.0" emptyText="其他费用调整金额"  class="mini-hidden" required="true" enabled="false" />

            <!-- 默认对账状态隐藏区域 -->
            <input id="transportCostAccountCheckStatusId" name="transportCostAccountCheckStatusId" value="431" emptyText="航空运费对账状态ID"  class="mini-hidden"  required="true" enabled="false" />
            <input id="groundAmountAccountCheckStatusId" name="groundAmountAccountCheckStatusId" value="431" emptyText="地面运费对账状态ID"  class="mini-hidden"  required="true" enabled="false" />
            <input id="airlinesAddCostAccountCheckStatusId" name="airlinesAddCostAccountCheckStatusId" value="431" emptyText="航空附加费对账状态ID"  class="mini-hidden"  required="true" enabled="false" />
            <input id="otherChargesAccountCheckStatusId" name="otherChargesAccountCheckStatusId" value="431" emptyText="其他费用费对账状态ID"  class="mini-hidden"  required="true" enabled="false" />




        </div>


        <script type="text/javascript">
            mini.parse();

            let masterBillForm = new mini.Form("#masterBillForm");

            let airlinesTwoCode = mini.get("airlinesTwoCode");
            // 承运方式
            let transportWayId = mini.get("transportWayId");

            // 外发单位
            let outUnitId = mini.get("outUnitId");

            // 航班日期
            let flightDate = mini.get("flightDate");

            // 航空公司
            let airlinesId = mini.get("airlinesId");

            // 配货方式
            let loadWayId = mini.get("loadWayId");

            // 货物类型
            let goodsTypeId = mini.get("goodsTypeId");

            // 始发机场
            let airportId = mini.get("airportId");

            // 目的市
            let destCityId = mini.get("destCityId");


            let flightNo = mini.get("flightNo");

            let flightName = mini.get("flightName");

            let planTakeOffTime = mini.get("planTakeOffTime");



            let billId = mini.get("billId");

            let ck1 = mini.get("ck1");

            let selectedBillDatagrid = mini.get("selectedBillDatagrid");
            let agentCompanyId = mini.get("agentCompanyId");
            let agentCompanyName = mini.get("agentCompanyName");
            let agentCompanyPhone = mini.get("agentCompanyPhone");
            let agentCompanyAddress = mini.get("agentCompanyAddress");

            let receiveCustomerId = mini.get("receiveCustomerId");
            let receiveCustomerName = mini.get("receiveCustomerName");
            let receiveCustomerPhone = mini.get("receiveCustomerPhone");
            let receiveCustomerAddress = mini.get("receiveCustomerAddress");

            let freightSpeciesId = mini.get("freightSpeciesId");
            let goodsCodeId = mini.get("goodsCodeId");
            let goodsName = mini.get("goodsName");
            let packingWayId = mini.get("packingWayId");
            let totalNumber = mini.get("totalNumber");
            let actualWeight = mini.get("actualWeight");
            let chargeableWeight = mini.get("chargeableWeight");
            let weightRate = mini.get("weightRate");
            let transportCost = mini.get("transportCost");

            let calcWeightCategoryId = mini.get("calcWeightCategoryId");
            let airportGoodsSiteId = mini.get("airportGoodsSiteId");
            let declaredValue = mini.get("declaredValue");
            let groundAmount = mini.get("groundAmount");
            let stampDuty = mini.get("stampDuty");
            let insuranceFee = mini.get("insuranceFee");
            let airlinesAddCost = mini.get("airlinesAddCost");

            let otherChargeDatagrid = mini.get("otherChargeDatagrid");
            let otherCharges = mini.get("otherCharges");
            let otherChargesComment = mini.get("otherChargesComment");

            let masterBillPaymentMethodId = mini.get("masterBillPaymentMethodId");
            let totalAmount = mini.get("totalAmount");
            let openMasterBillDate = mini.get("openMasterBillDate");
            let openMasterBillDepartmentId = mini.get("openMasterBillDepartmentId");
            let openMasterBillUserId = mini.get("openMasterBillUserId");
            let comment = mini.get("comment");
            let dropAmount = mini.get("dropAmount");
            let settlementAmount = mini.get("settlementAmount");

            // 实际配载重量,用于成本分摊
            let actualLoadTotalWeight = mini.get("actualLoadTotalWeight");




            // 定义外发或自发
            let transportWay = {self:81, out:82};

            // 定义配货方式
            let loadWay = {merge:181, alone:182};

            // 定义计算重量分类
            let calcCategory = {actualt:61,chargeable:62};

            // 定义结算
            let settlementStatus ={uncleared:321, cleared:322};

            // 定义选择的航空公司两字码的值
            let selectedTwoCodeValue="";

            // 航空公司附加费用列表数据
            let airlinesAddCostDetailList = "";

            // 定义已经加入的分单ID
            let billIdArray=[];

            // 定义分单位检索的默认条件
            let baseUrl = "${base}/pages/native_bill_manage/getBNativeBillPojoByCnd?transportSpeciesId=71&pullDisposeId=232";

            // 获取操作类型
            let opType = "${opType!}";

            $(function (){

                // 航空公司两字码
                let airlinesIdJsonData = commonRenderDropList("airlines");
                airlinesTwoCode.load(airlinesIdJsonData);
                // 加载航空公司下拉列表
                airlinesId.load(airlinesIdJsonData);

                // 配货方式
                let loadWayIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_LOAD_WAY");
                loadWayId.load(loadWayIdJsonData);

                // 承运方式下拉列表
                let transportWayIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_TRANSPORT_WAY");
                transportWayId.load(transportWayIdJsonData);

                // 加载外发单位下拉列表
                let outUnitIdJsonData = commonRenderDropList("outUnit");
                outUnitId.load(outUnitIdJsonData);

                // 加载目的城市下拉列表
                let destCityIdJsonData=commonRenderDropList("city");
                destCityId.loadList(destCityIdJsonData,"id","pid");

                // 货物类型下拉列表
                let goodsTypeIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_GOODS_TYPE");
                goodsTypeId.load(goodsTypeIdJsonData);

                // 始发机场下拉列表
                let airportIdJsonData = commonRenderDropList("airport");
                airportId.load(airportIdJsonData);

                // 运价种类下拉列表
                let freightSpeciesIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_FREIGHT_SPECIES");
                freightSpeciesId.load(freightSpeciesIdJsonData);

                // 商品代号下拉列表
                let goodsCodeIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_GOODS_CODE");
                goodsCodeId.load(goodsCodeIdJsonData);

                // 包装方式下拉列表
                let packingWayIdJsonData=commonRenderSysDicDropListByKey("sysDic","XT_DIC_PACKING_WAY");
                packingWayId.load(packingWayIdJsonData);

                // 计算运价分类
                let calcWeightCategoryIdJsonData= commonRenderSysDicDropListByKey("sysDic","XT_DIC_CALC_WEIGHT_CATEGORY");
                calcWeightCategoryId.load(calcWeightCategoryIdJsonData);

                // 航空主单付款方式 XT_DIC_AIR_MASTER_BILL_PAYMENT_METHOD
                let masterBillPaymentMethodIdJsonData= commonRenderSysDicDropListByKey("sysDic","XT_DIC_AIR_MASTER_BILL_PAYMENT_METHOD");
                masterBillPaymentMethodId.load(masterBillPaymentMethodIdJsonData);

                // 开单员数据
                let userJsonData = commonRenderDropList("sysUser");
                openMasterBillUserId.load(userJsonData);

                // 加载开单部门
                let departmentJsonData = commonRenderDropList("department");
                openMasterBillDepartmentId.loadList(departmentJsonData,"id","pid");
                openMasterBillDepartmentId.setValue("${openMasterBillDepartmentId!}");

                if(opType=="add"){
                    // 设置默认值
                    setDefaultValue()
                }
                else{
                    // 这只是查看,需要隐藏保存按钮
                    if(opType=="view"){
                        mini.get("saveBtn").hide();
                    }

                    // 这是编辑
                    /**
                     * 1.锁定主单号
                     */
                    airlinesTwoCode.setEnabled(false);
                    mini.get("id").setEnabled(false);
                    airlinesId.setEnabled(false);

                    // 加载航班列表
                    flightNo.load(${bFlightList!});


                    // 根据运输方式加载收货人  pojo.getLoadWayId();
                    let loadWayIdValue = public_toNumber(${pojo.loadWayId!});
                    // 如果是单独开单
                    if(loadWayIdValue == loadWay.alone){
                        let receiveCustomerArray=[];
                        let receiveCustomer={};
                        receiveCustomer.id="${pojo.receiveCustomerId!}";
                        receiveCustomer.name = "${pojo.receiveCustomerName!}";
                        receiveCustomer.phone = "${pojo.receiveCustomerPhone!}";
                        receiveCustomer.address = "${pojo.receiveCustomerAddress!}";
                        receiveCustomerArray.push(receiveCustomer);
                        receiveCustomerId.load(receiveCustomerArray);
                    }else{
                        // 这是合大票
                        let destCityIdValue = '${pojo.destCityId!}';
                        processLoadMerge(loadWayIdValue, destCityIdValue);
                    }

                    // 处理货站下拉列表goodsSiteList
                    airportGoodsSiteId.load(${goodsSiteList!});

                    // 设定表单数据
                    masterBillForm.setData(${pojo!});

                    // 截取主单号前面的两位字母
                    let tempId = mini.get("id").getValue();
                    tempId = tempId.substr(2,tempId.length-1);
                    mini.get("id").setValue(tempId);

                    // 加载货站
                    loadAirportGoodsSite();

                    flightDate.setValue("${pojo.flightDate!}");
                    openMasterBillDate.setValue("${pojo.openMasterBillDate!}");
                    // 设置两字码的选择 ID
                    airlinesTwoCode.setValue(${pojo.airlinesId!})

                    // 控制外发单位是否显示
                    transportWayIdChange();

                    // 编辑时分单选择约束默认打开不做控制
                    ck1.setEnabled(true);

                    billIdArray.splice(0,billIdArray.length);

                    // 加载主单中包含的分单
                    let loadBNativeBillPojoListJSONStr = '${loadBNativeBillPojoList!}';
                    let loadBNativeBillPojoList = JSON.parse(loadBNativeBillPojoListJSONStr);
                    if(!public_isEmpty(loadBNativeBillPojoList)){
                        if(loadBNativeBillPojoList.length>0){
                            for(let billObj of loadBNativeBillPojoList){
                                billIdArray.push(billObj.id);
                                selectedBillDatagrid.addRow(billObj);
                            }
                        }
                    }

                    // 修改检索分单框的下拉列表查询条件
                    setBillSearchCndUrl(flightNo.getValue());

                    // 加载主单中包含的其他费用
                    let pojoOtherChargeListStr = '${pojoOtherChargeList!}';
                    let pojoOtherChargeList = JSON.parse(pojoOtherChargeListStr);
                    if(!public_isEmpty(pojoOtherChargeList)){
                        if(pojoOtherChargeList.length>0){
                            for(let otherChargeObj of pojoOtherChargeList){
                                otherChargeDatagrid.addRow(otherChargeObj);
                            }
                        }
                    }

                    // 加载航空公司附加费用列表,并放到声明的变量中
                    let airlinesIdValue = airlinesId.getSelected().id;
                    loadAirlinesAddCostDetailList(airlinesIdValue);


                    // 处理代理公司加载列表，直接构建就好 pojo.getAgentCompanyId()
                    loadAgentCompany("${pojo.agentCompanyId!}");

                }
            });


            /**
             * 设置控件对象默认值
             */
            function setDefaultValue(){
                loadWayId.setValue(loadWay.alone);
                transportWayId.select(0);
                goodsTypeId.select(0);
                freightSpeciesId.select(0);
                goodsCodeId.select(0);
                calcWeightCategoryId.select(1);

                showOrHideOutUnit(transportWay.self);

                // 设置隐藏区域默认值
                setObjValue(mini.get("transportCostStatusId"),321);
                setObjValue(mini.get("groundAmountStatusId"),321);

                setObjValue(mini.get("dropAmount"),0.0);
                setObjValue(mini.get("settlementAmount"),0.0);
                setObjValue(mini.get("dropStatusId"),361);
                setObjValue(mini.get("settlementStatusId"),321);


            }



            /**
             * 航空公司两字码选择变化
             */
            function airlinesTwoCodeChange(e){


                let seletedItem = e.selected;

                // 设置航空公司，并锁定
                airlinesId.setValue(seletedItem.id);
                //airlinesId.setEnabled(false);
                // 保存已经选择的两字码
                selectedTwoCodeValue = seletedItem.twoCode;

                // 加载货站列表
                // airPortId  airlinesId  calcWeightCategoryId
                loadAirportGoodsSite();

                // 加载航空公司附加费用列表,并放到声明的变量中
                loadAirlinesAddCostDetailList(seletedItem.id);
            }

            function loadAirlinesAddCostDetailList(airlinesIdValue){
                // 加载航空公司附加费用列表,并放到声明的变量中
                airlinesAddCostDetailList = public_getServerData("${base}/pages/basic_data_manage/xt_airlines/add_cost_detail/queryAddCostDetailListByAirlinesId?airlinesId=" + airlinesIdValue);

            }

            /**
             * 计算航空附加费
             */
            function calcAirlinesAddCost(){
                let calcWightCategoryValue = calcWeightCategoryId.getValue();
                let weightValue = public_toNumber(chargeableWeight.getValue());
                if(calcWightCategoryValue==calcCategory.actualt){
                    weightValue = public_toNumber(actualWeight.getValue());
                }
                // 遍历airlinesAddCostDetail
                let addCostTotal=0;

                let transportWayIdValue = transportWayId.getValue();
                if(transportWayIdValue == transportWay.self){
                    // 重量大于零才需要计算，如果不判断会在重量为零时使用航空附加费的起票价
                    if(weightValue>0){
                        if(!public_isEmpty(airlinesAddCostDetailList)){
                            for(let costDetail of airlinesAddCostDetailList){
                                // 1.取出起票价
                                let ticketPrice=public_toNumber(costDetail.ticketPrice,0);
                                // 2.得到单价
                                let price = public_toNumber(costDetail.price,0);
                                // 3.计算价格
                                let weightTotalPrice = public_round2(price * weightValue);
                                if(weightTotalPrice > ticketPrice){
                                    addCostTotal = public_decimal(addCostTotal + weightTotalPrice,0);
                                }
                                else{
                                    addCostTotal = public_decimal(addCostTotal + ticketPrice,0);
                                }
                            }
                        }
                    }
                }
                airlinesAddCost.setValue(addCostTotal);
            }

            /**
             * 根据选择的运价分类，加载相应的货站信息
             */
            function calcWeightCategoryIdChange(){
                loadAirportGoodsSite();
            }

            /**
             * 加载机场货站列表
             */
            function loadAirportGoodsSite(){
                if(isEmptyNoAlert(airportId)){
                    return;
                }
                if(isEmptyNoAlert(airlinesId)){
                    return;
                }
                if(isEmptyNoAlert(calcWeightCategoryId)){
                    return;
                }
                let airPortIdValue = airportId.getValue();
                let airlinesIdValue = airlinesId.getValue();
                let calcWeightCategoryIdValue = calcWeightCategoryId.getValue();
                let outUnitIdValue = outUnitId.getValue();

                let param="keyAirportId=" + airPortIdValue;
                param += "&keyAirlinesId=" + airlinesIdValue;
                param += "&calcWeightCategoryId=" + calcWeightCategoryIdValue;
                param += "&outUnitId" + outUnitIdValue;


                let ret = public_getServerData("${base}/pages/basic_data_manage/xt_airport/goods_site/queryNoPage?" + param);
                if(ret.length>0){
                    // 绑定机场货站结果
                    airportGoodsSiteId.load(ret);
                    return;
                }
                airportGoodsSiteId.load();
            }


            /**
             * 根据选择的货站自动计算地面运费
             */
            function airportGoodsSiteIdChange(){
                if(isEmpty(goodsTypeId,"货物类型")){
                    return;
                }
                if(isEmptyNoAlert(airportGoodsSiteId)){
                     return;
                }
                let selectItem =  airportGoodsSiteId.getSelected();
                let airportGoodsSiteIdValue= selectItem.id;
                let goodsTypeIdValue = goodsTypeId.getValue();

                let calcWightCategoryValue = calcWeightCategoryId.getValue();
                let weightValue = public_toNumber(chargeableWeight.getValue());
                if(calcWightCategoryValue==calcCategory.actualt){
                    weightValue = public_toNumber(actualWeight.getValue());
                }

                let totalAmount=0;

                let transportWayIdValue = transportWayId.getValue();

                // 是自发需要计算地面运费，外发是全包价不需要计算
                if(transportWayIdValue == transportWay.self){
                    // 重量大于零才需要计算地面运费用，直接计算会取地面运费的起票价，这是不合理的
                    if(weightValue>0){
                        let ret = public_getServerData("${base}/pages/basic_data_manage/xt_airport/goods_site/detail/queryGoodsSiteDetails",{airportGoodsSiteId:airportGoodsSiteIdValue, goodsTypeId:goodsTypeIdValue});

                        if(!public_isEmpty(ret) && ret.length>0){
                            //price 地面运费单价
                            //ticketPrice 地面运费起票价
                            for(let detail of ret){
                                let ticketPrice = detail.ticketPrice;
                                let price = detail.price;
                                let tempTotal = public_round2(price * weightValue);
                                totalAmount += ticketPrice>tempTotal?ticketPrice:tempTotal;
                            }
                        }
                    }
                }

                // 设置地面运费
                groundAmount.setValue(totalAmount);
            }

            /**
             * 主单号变化
             */
            function idChange(){

                let transportWayIdValue = transportWayId.getValue();
                if(transportWayIdValue == transportWay.out){
                    return;
                }

                if(isEmpty(airlinesTwoCode,"航司两字码")){
                    mini.get("id").setValue();
                    return;
                }

                // 清空发货人数据
                agentCompanyId.load([{}]);
                agentCompanyName.setValue();
                agentCompanyPhone.setValue();
                agentCompanyAddress.setValue();

                let idValue = mini.get("id").getValue();
                let masterBillCodeIdValue = selectedTwoCodeValue + idValue;
                if(!public_isEmpty(idValue)){
                    // 发送请求，返回主单号对象
                    let ret = public_getServerData("${base}/pages/bill_code_manage/b_master/queryMasterBillCodeById?id=" + masterBillCodeIdValue);
                    if(!ret.ok){
                        showAlert("主单号 [" + masterBillCodeIdValue + "] 不存在，请更换其他主单号!");
                        mini.get("id").setValue();
                        return;
                    }

                    let useStatus = {unused:241, used:242, cancelled:243};
                    if(ret.data.useStatusId == useStatus.unused){
                        // 可以使用
                        // 1.从主单号中带出代理公司信息
                        loadAgentCompany(ret.data.agentCompanyId)
                    }
                    else if(ret.data.useStatusId == useStatus.used){
                        // 已经被使用
                        showAlert("主单号 [" + masterBillCodeIdValue + "] 已被使用，请更换其他主单号!");
                        mini.get("id").setValue();
                    }
                    else if(ret.data.useStatusId == useStatus.cancelled){
                        // 已经作废
                        showAlert("主单号 [" + masterBillCodeIdValue + "] 已被作废，不能使用!");
                        mini.get("id").setValue();
                    }
                }
            }

            /**
             * 根据主单号加载代理公司信息
             */
            function loadAgentCompany(agentCompanyIdValue){
                // 加载公司信息
                let agentCompanyJsonData = commonRenderDropList("sysCompany");
                let jsonArray = [];
                // 是自发，则保留一个固定值
                if(transportWayId.getValue()==transportWay.self){
                    for(let obj of agentCompanyJsonData){
                        if(obj.id == agentCompanyIdValue){
                            jsonArray.push(obj);
                            break;
                        }
                    }
                }
                else{
                    jsonArray = agentCompanyJsonData;
                }

                agentCompanyId.load(jsonArray);



                let selectIndex = -1;
                let selectName;
                let selectPhone;
                let selectRegisteredAddress;

                for(let i=0;i<jsonArray.length;i++){
                    let obj = jsonArray[i];
                    if(obj.id==agentCompanyIdValue){
                        selectIndex=i;
                        selectName=obj.name;
                        selectPhone=obj.phone;
                        selectRegisteredAddress=obj.registeredAddress;
                        break;
                    }
                }

                if(selectIndex!=-1){
                    agentCompanyId.select(selectIndex);
                    agentCompanyName.setValue(selectName);
                    agentCompanyPhone.setValue(selectPhone);
                    agentCompanyAddress.setValue(selectRegisteredAddress);
                }
            }

            // XT_DIC_PULL_STATUS
            /**
             * NO_PULL(361, "未拉货", "未拉货"),
             PULL(362, "已拉货", "已拉货");
             */
            let pullStatus = {noPull:361,pull:362};

            /**
             * 保存空运主单
             */
            function saveMasterBill(){
                masterBillForm.validate();
                if (masterBillForm.isValid() == false) {
                    return;
                }

                //提交数据
                let masterBillPojo = masterBillForm.getData(true);

                // settlementStatusId 如果主单已经结算，则不允许做任何调整,给出提示
                let settlementStatusId = masterBillPojo.settlementStatusId;
                if(settlementStatusId == settlementStatus.cleared){
                    showAlert("主单已结算，不能做保存操作!");
                    return;
                }

                // 拉过货的主单不能保存
                let dropStatusId = masterBillPojo.dropStatusId;
                if(dropStatusId == pullStatus.pull){
                    showAlert("主单已做过拉货，不能进行更新修改保存操作!");
                    return;
                }

                if(masterBillPojo.chargeableWeight<=0){
                    showAlert("主单计费重量不能小于或等于零!");
                    return;
                }

                masterBillPojo.billId = airlinesTwoCode.getText() + masterBillPojo.id;

                // 获取其他费用列表
                let otherChargeList=otherChargeDatagrid.getData();
                masterBillPojo.otherChargeList = otherChargeList;


                // 获取主单包含的分单信息
                let nativeBillList = selectedBillDatagrid.getData();

                if(nativeBillList.length==0){
                    showAlert("主单下不包含分单，不能保存主单!");
                    return;
                }

                // if(nativeBillList.length == 1){
                //     if(loadWayId.getValue() !=  loadWay.alone){
                //         showAlert("分单列表中只有一个分单，配货方式请选择【单独开单】!");
                //         return;
                //     }
                // }
                //
                // if(nativeBillList.length > 1){
                //     if(loadWayId.getValue() !=  loadWay.merge){
                //         showAlert("分单列表中只有多个分单，配货方式请选择【合大票】!");
                //         return;
                //     }
                // }

                masterBillPojo.nativeBillList = nativeBillList;

                let opUrl ="${base}/pages/master_bill_manage/flight/addMasterFlightBill";
                if(opType=="update"){
                    opUrl ="${base}/pages/master_bill_manage/flight/updateMasterFlightBill";
                }

                mini.confirm("确定保存主单信息?","保存提示",function (action){
                    if (action == "ok") {
                        let msgId = mini.loading("正在保存, 请稍等 ...", "保存中");
                        let ret = public_getServerData(opUrl,masterBillPojo);
                        mini.hideMessageBox(msgId);
                        if(opType=="update"){
                            showAlert(ret.message,function (){
                                if(ret.ok){
                                    // 关闭编辑页面
                                    parent.window.closeActiveTab();
                                }
                            });
                        }
                        else{
                            showAlert(ret.message,function (){
                                if(ret.ok){
                                    // 刷新当前页面
                                    window.location.reload();
                                }
                            });
                        }
                    }
                });
            }

            /**
             * 公司选择改变
             */
            function agentCompanyIdChange(e) {
                let selectedItem = e.selected;
                agentCompanyAddress.setValue(selectedItem.registeredAddress);
                agentCompanyPhone.setValue(selectedItem.phone);
                agentCompanyName.setValue(selectedItem.name);
            }


            /**
             * 运输方式选择值的变化
             */
            function transportWayIdChange(){
                let transportWayIdValue = transportWayId.getValue();
                showOrHideOutUnit(transportWayIdValue);

                agentSet(transportWayIdValue);

                if(transportWayIdValue == transportWay.out){
                    // 加载公司信息
                    let agentCompanyJsonData = commonRenderDropList("sysCompany");
                    agentCompanyId.load(agentCompanyJsonData);
                    // 锁定
                    lockedAirCost(false);
                }
                else{
                    // 这个地方要清空外发

                    lockedAirCost(true);
                }
            }


            /**
             * 锁定相应的广本框并设置为0
             * declaredValue   groundAmount  stampDuty  insuranceFee  airlinesAddCost
             */
            function lockedAirCost(openFlag) {
                // declaredValue   groundAmount  stampDuty  insuranceFee  airlinesAddCost
                declaredValue.set({enabled:openFlag});
                groundAmount.set({enabled:openFlag});
                stampDuty.set({enabled:openFlag});
                insuranceFee.set({enabled:openFlag});
                airlinesAddCost.set({enabled:openFlag});
                if(openFlag){
                    mini.get("calcAirlinesAddCostBtn").show();
                }
                else{
                    mini.get("calcAirlinesAddCostBtn").hide();
                    declaredValue.setValue(0);
                    groundAmount.setValue(0);
                    stampDuty.setValue(0);
                    insuranceFee.setValue(0);
                    airlinesAddCost.setValue(0);
                }
            }




            /**
             * 发货人设置是否必须状态设置
             * 外发时发货人不是必须要填写的
             */
            function agentSet(value){

                agentCompanyId.load();
                // 先清空
                agentCompanyAddress.setValue();
                agentCompanyPhone.setValue();
                agentCompanyName.setValue();
                agentCompanyId.setValue();

                agentCompanyId.set({required:true});
                agentCompanyName.set({required:true});
                agentCompanyPhone.set({required:true});
                agentCompanyAddress.set({required:true});
            }



            /**
             * 根据选择的运输方式控制外发单位是否显示
             */
            function showOrHideOutUnit(value){
                if(value==transportWay.self){
                    // 自发
                    // 1.清空外发单位选择
                    // 2.隐藏外发单拉标题及下拉选择控件
                    $("#outUnitTitle").hide();
                    outUnitId.setValue();
                    outUnitId.setRequired(false);
                    outUnitId.hide();

                }
                else{
                    // 外发
                    // 1.显示外发标题
                    $("#outUnitTitle").show();
                    outUnitId.setRequired(true);
                    outUnitId.show();
                }
            }

            /**
             * 目的市选择
             */
            function destCityIdChange(){
                // 清空始发机场与航班信息
                airportId.setValue();
                clearFlightInfo();
                // 加载一下合票提货人信息
                loadMerge();
            }

            /**
             * 始发机场变化
             */
            function airportIdChange(){
                // 清空航班信息
                clearFlightInfo();
                // 重新加载货站
                loadAirportGoodsSite();
            }

            /**
             * 清空航班信息
             */
            function clearFlightInfo(){
                // 1.清空航班日期
                flightDate.setValue();
                // 2.清空航班号
                flightNo.load();
                flightName.setValue();
                // 3.设置开放约束
                ck1.setValue(false);
                ck1.setEnabled(false);
            }

            /**
             * 航班日期调整，根据选择的航班得到相应的下拉列表,绑定到航班选择下拉列表框
             */
            function flightDateChanged(){
                /**
                 * 航班选择条件，目的省或目的市id,机场id，航空公司id,航班日期
                 */

                if(isEmpty(destCityId,"目的市")){
                    flightDate.setValue();
                    return ;
                }

                if(isEmpty(airportId,"始发机场")){
                    flightDate.setValue();
                    return;
                }

                if(isEmpty(airlinesId,"航空公司")){
                    flightDate.setValue();
                    return;
                }

                if(isEmpty(flightDate,"航班日期")){
                    flightDate.setValue();
                    return;
                }

                let destCityIdValue = destCityId.getValue();
                let airportIdValue = airportId.getValue();
                let airlinesIdValue = airlinesId.getValue();
                let flightDateValue = flightDate.getFormValue();

                // 加载后直接返回List
                let ret = public_getServerData("${base}/pages/flight_manage/queryFlightNoListByDropCnd",
                    {
                        cityId : destCityIdValue,
                        airportId : airportIdValue,
                        airlinesId : airlinesIdValue,
                        flightDate : flightDateValue
                    });
                // 绑定数据
                flightNo.load(ret);
                // 清空一下航班名称
                flightName.setValue();
                // 设置开放条件
                ck1.setValue(false);
                ck1.setEnabled(false);
            }


            /**
             * 航班号选择改变
             */
            function flightNoChanged(){
                let flightNoObj = flightNo.getSelected();
                flightName.setValue(flightNoObj.name);
                planTakeOffTime.setValue(flightNoObj.planTakeOffTime);

                // 设置billId的url
                setBillSearchCndUrl(flightNoObj.id);

                ck1.setValue(false);
                ck1.setEnabled(true);
            }

            function setBillSearchCndUrl(flightNoValue){
                let billIdUrl = baseUrl;
                let destCityIdValue = destCityId.getValue();
                let airportIdValue = airportId.getValue();
                let airlinesIdValue = airlinesId.getValue();
                let flightDateValue = flightDate.getFormValue();
                billIdUrl += "&destCityId=" + destCityIdValue;
                billIdUrl += "&airportId=" + airportIdValue;
                billIdUrl += "&airlinesId=" + airlinesIdValue;
                billIdUrl += "&flightDate=" + flightDateValue;
                billIdUrl += "&flightNo=" + flightNoValue;
                billIdUrl += "&flightName=" + flightName.getValue();

                let existBillIds = "";
                if(billIdArray.length>0){
                    for(let billIdValue of billIdArray){
                        existBillIds += billIdValue + ",";
                    }
                    billIdUrl += "&existBillIds=" + existBillIds;
                }

                billId.setUrl(billIdUrl);
            }

            /**
             * 是否放开约束条件
             */
            function ck1Changed(e){
                let checked = this.getChecked();
                if(checked){
                    billId.setUrl(baseUrl);
                }
                else{
                    setBillSearchCndUrl(flightNo.getValue());
                }
            }


            /**
             * 删除选择
             */
            function flightOnCloseClick(){
                flightName.setValue();
            }

            /**
             * 分单号输入框获取焦点时发生
             */
            function billIdFocus(){
                // 检测主单上的 目的省 目的市 始发机场  航空公司  航班日期 航班号是否已经选择
                if(isEmpty(destCityId,"目的市")){
                    return ;
                }

                if(isEmpty(airportId,"始发机场")){
                    return ;
                }

                if(isEmpty(airlinesId,"航空公司")){
                    return ;
                }

                if(isEmpty(flightDate,"航班日期")){
                    return ;
                }

                if(isEmpty(flightNo,"航班号")){
                    return ;
                }
                // 获取焦点时就设置其检索条件值
                setBillSearchCndUrl(flightNo.getValue());
            }

            /**
             * 分单下拉列表的选择
             */
            function billIdChanged(e){
                let selectedItem = e.selected;

                if(public_isEmpty(selectedItem)){
                    billId.setValue();
                    return;
                }


                // 获取配货方式值
                let loadWayIdValue = loadWayId.getValue();

                // 获取已经选择的列表框中的所有选择
                let array = selectedBillDatagrid.data;
                let isExist=false;
                if(array.length>0){
                    if(loadWayIdValue == loadWay.alone){
                        billId.setValue();
                        showAlert("配货方式是 [单独开单] 不能添加多个分单");
                        return;
                    }
                    for(let i=0;i<array.length;i++){
                        let obj = array[i];
                        if(obj.id == selectedItem.id){
                            isExist = true;
                            break;
                        }
                    }
                    if(isExist){
                        billId.setValue();
                        showAlert(selectedItem.id + " 分单已经存在于列表中，不能重复添加");
                        return;
                    }
                }
                // 添加进去
                selectedBillDatagrid.addRow(selectedItem);

                // 如果是单独开单，把分单的上的收货人信息取出来，填写到主单的收货人信息区中
                if(loadWayIdValue == loadWay.alone){
                    let receiveCustomerArray=[];
                    let receiveCustomer={};
                    receiveCustomer.id=1;
                    receiveCustomer.name = selectedItem.receiveCustomerName;
                    receiveCustomer.phone = selectedItem.receiveCustomerPhone;
                    receiveCustomer.address = selectedItem.receiveCustomerAddress;
                    receiveCustomerArray.push(receiveCustomer);
                    receiveCustomerId.load(receiveCustomerArray);
                    receiveCustomerId.select(1);
                    receiveCustomerName.setValue(receiveCustomer.name);
                    receiveCustomerPhone.setValue(receiveCustomer.phone);
                    receiveCustomerAddress.setValue(receiveCustomer.address);

                    goodsName.setValue(selectedItem.goodsName);
                    packingWayId.setValue(selectedItem.packingWayId);
                }

                // 把添加的分单加入主单的配载列表中
                setMasterBillLoadNativeBillIds();

                // 总值累计
                sumBillDatagrid();

                // 修改检索分单框的下拉列表查询条件
                setBillSearchCndUrl(flightNo.getValue());
            }

            // 把添加的分单加入主单的配载列表中
            function setMasterBillLoadNativeBillIds(){
                let loadBillArray = selectedBillDatagrid.data;
                let loadNativeBillIdsValue = "";
                for(let obj of loadBillArray){
                    loadNativeBillIdsValue += obj.id + ",";
                }
                if(loadNativeBillIdsValue.length>0){
                    loadNativeBillIdsValue = loadNativeBillIdsValue.substr(0,loadNativeBillIdsValue.length-1);
                }
                mini.get("loadNativeBillIds").setValue(loadNativeBillIdsValue);
            }


            /**
             * 总值累计
             */
            function sumBillDatagrid(){
                // 清空数组
                billIdArray.splice(0,billIdArray.length);
                // 求和计算
                let sumArray = selectedBillDatagrid.data;
                let sumTotalNumber=0;
                let sumActualWeight=0;
                let sumChargeableWeight=0;
                let sumActualLoadTotalWeight=0;
                let sumVolume=0;
                if(sumArray.length>0){
                    for(let i=0;i<sumArray.length;i++){
                        billIdArray.push(sumArray[i].id);
                        sumTotalNumber += sumArray[i].totalNumber;
                        sumActualWeight += sumArray[i].actualWeight;
                        sumChargeableWeight += sumArray[i].chargeableWeight;
                        sumActualLoadTotalWeight += sumArray[i].actualWeight;
                        sumVolume += sumArray[i].volume;
                    }
                }

                totalNumber.setValue(public_round2(sumTotalNumber));
                actualWeight.setValue(public_round2(sumActualWeight));
                chargeableWeight.setValue(public_round2(sumChargeableWeight));
                actualLoadTotalWeight.setValue(public_round2(sumActualLoadTotalWeight));

                // 根据费率计算运输费用
                calcTransportCost();
            }



            function gridCellEndEdit(e){
                selectedBillDatagrid.data[e.rowIndex][e.field] = public_toNumber(e.value);
                sumBillDatagrid();
            }
            /**
             * 配货方式变化时，清空已经选择的分单列表
             */
            function loadWayIdChange(){
                // 清空分单号
                billId.setValue();
                // 清空分单列表
                selectedBillDatagrid.clearRows();

                // 加载一下合票提货信息
                loadMerge()
            }

            function loadMerge(){
                // 清空一下
                receiveCustomerId.load();
                // 如果是合大票的配货方式，则加载航空类型的提货点，否则清空
                let loadWayIdValue = loadWayId.getValue();
                let destCityIdValue = destCityId.getValue();
                // 合大票时收货人下拉列表处理
                processLoadMerge(loadWayIdValue, destCityIdValue);

                // 清空下收货人信息
                receiveCustomerId.setValue();
                receiveCustomerName.setValue();
                receiveCustomerPhone.setValue();
                receiveCustomerAddress.setValue();
            }

            /**
             * 合大票时收人处理
             */
            function processLoadMerge(loadWayIdValue,destCityIdValue){
                if(loadWayIdValue==loadWay.merge){
                    // 加载航空类型的合票提货点
                    let pickUpPointArray = commonRenderDropList("pickUpPoint");
                    let airPickUpPointArray=[];
                    let pickUpPointType={air:31,auto:32};
                    if(pickUpPointArray.length>0){
                        for(let i=0;i<pickUpPointArray.length;i++){
                            // 要求是空运，而且其目的市与主单上的目的省或目的市id 相同
                            if(pickUpPointArray[i].pickUpTypeId==pickUpPointType.air && pickUpPointArray[i].cityId==destCityIdValue){
                                airPickUpPointArray.push(pickUpPointArray[i]);
                            }
                        }
                        receiveCustomerId.load(airPickUpPointArray);
                    }
                }
            }

            /**
             * 删除选择的分单
             */
            function removeSelectedRow(){
                let selectedRows = selectedBillDatagrid.getSelecteds();
                selectedBillDatagrid.removeRows(selectedRows);
                // 总值累计
                sumBillDatagrid();
                billId.setValue("");

                // 把添加的分单加入主单的配载列表中
                setMasterBillLoadNativeBillIds();
            }

            /**
             * 设置选择的提货人信息
             * @param e
             */
            function receiveCustomerIdChanged(){
                if(!isEmptyNoAlert(receiveCustomerId)){
                    let seletedItem = receiveCustomerId.getSelected();
                    receiveCustomerName.setValue(seletedItem.name);
                    receiveCustomerPhone.setValue(seletedItem.phone);
                    receiveCustomerAddress.setValue(seletedItem.address);
                    return;
                }
                receiveCustomerId.setValue();
                receiveCustomerName.setValue();
                receiveCustomerPhone.setValue();
                receiveCustomerAddress.setValue();

            }

            /**
             * 增加其他费用
             */
            function addOtherChargesRow(){
                let newRow = {};
                otherChargeDatagrid.addRow(newRow);
                otherChargeDatagrid.beginEditCell(newRow, "name");
            }

            /**
             * 移除其他费用
             */
            function removeOtherChargesRow(){
                let rows = otherChargeDatagrid.getSelecteds();
                if (rows.length > 0) {
                    otherChargeDatagrid.removeRows(rows, true);
                    // 重新计算
                    calcOtherCharges();
                }
            }

            /**
             * 计算其他费用
             */
            function calcOtherCharges(){
                let otherChargeArray=otherChargeDatagrid.getData();

                if(otherChargeArray.length > 0){
                    let otherChargeTotal = 0;
                    let otherChargeComment = "";
                    let otherChargeFlag = false;
                    for(let obj of otherChargeArray){
                        if(public_isEmpty(obj.name)){
                            showAlert("【其他费用】【名目】没有填写！");
                            return;;
                        }

                        let tempAmount = public_toNumber(obj.amount,-1);

                        if(tempAmount == -1){
                            showAlert("【其他费用】【金额】没有填写或填写了非数字值！");
                            return;;
                        }

                        let comment = obj.comment;
                        if(public_isEmpty(comment)){
                            comment = "";
                        }

                        otherChargeTotal += tempAmount;
                        otherChargeComment += "{其他费用名目:" + obj.name + ",";
                        otherChargeComment += "金额:" + tempAmount + ",";
                        otherChargeComment += "备注:" + comment + "} ";
                    }

                    otherCharges.setValue(otherChargeTotal);
                    otherChargesComment.setValue(otherChargeComment);

                }
                else{
                    // 清空其他费用金额,及其他费用备注
                    otherCharges.setValue(0);
                    otherChargesComment.setValue("");
                }

                // 计算总金额
                masterBillPaymentMethodIdChange();
            }

            /**
             * 实际重量值改变
             */
            function actualWeightChanged() {
                // calcCost();
            }

            /**
             * 计费重量值改变
             */
            function chargeableWeightChanged() {
                calcCost();
            }

            /**
             * 重量费率值改变
             */
            function weightRateChanged(){
                calcCost();
            }

            function calcCost() {
                if(isEmpty(weightRate,"重量费率") || isEmpty(actualWeight,"实际重量") || isEmpty(chargeableWeight,"计费重量")){
                    transportCost.setValue(0);
                    return;
                }
                if(isNonNumeric(weightRate,"重量费率") || isNonNumeric(actualWeight,"实际重量") || isNonNumeric(chargeableWeight,"计费重量")){
                    transportCost.setValue(0);
                    return;
                }

                // 计算航空运费
                calcTransportCost()
            }

            /**
             * 计算航空运费
             */
            function calcTransportCost(){
                let weightRateValue = public_toNumber(weightRate.getValue());
                let actualWeightValue = public_toNumber(actualWeight.getValue());
                let chargeableWeightValue = public_toNumber(chargeableWeight.getValue());

                //let transportCostValue = weightRateValue * actualWeightValue > weightRateValue * chargeableWeightValue?public_round2(weightRateValue*actualWeightValue):public_round2(weightRateValue*chargeableWeightValue);

                // 航空运费就是用费率乘以计费重量
                let transportCostValue = public_decimal(weightRateValue*chargeableWeightValue,0);

                transportCost.setValue(transportCostValue);

                // 计算一下地面运费用
                airportGoodsSiteIdChange();
                // 计算一下航空附加费
                calcAirlinesAddCost();

                // 计算一下总金额
                masterBillPaymentMethodIdChange();
            }

            /**
             * 选择付款方式，计算出总金额
             * 总金额=航空运费+地面运费+印花税+保险费+航空附加费+其他费用（属于支出费用）
             */
            function masterBillPaymentMethodIdChange(){
                /**
                 * transportCost 航空运费
                 declaredValue 声明价值附加费
                 groundAmount 地面运费
                 stampDuty  印花税
                 insuranceFee  保险费
                 airlinesAddCost 航空附加费
                 otherCharges 其他费用
                 */
                if(isEmpty(transportCost,"航空运费")
                    || isEmpty(groundAmount,"地面运费")
                    || isEmpty(stampDuty,"印花税")
                    || isEmpty(insuranceFee,"保险费")
                    || isEmpty(airlinesAddCost,"航空附加费")
                    || isEmpty(otherCharges,"其他费用")){
                    masterBillPaymentMethodId.setValue();
                    totalAmount.setValue(0);
                    return;
                }

                if(isEmptyNoAlert(masterBillPaymentMethodId)){
                    totalAmount.setValue(0);
                    masterBillPaymentMethodId.setValue();
                    return;
                }

                // 计算总金额
                let transportCostValue = public_toNumber(transportCost.getValue(),0);
                let groundAmountValue = public_toNumber(groundAmount.getValue(),0);
                let stampDutyValue = public_toNumber(stampDuty.getValue(),0);
                let insuranceFeeValue = public_toNumber(insuranceFee.getValue(),0);
                let airlinesAddCostValue = public_toNumber(airlinesAddCost.getValue(),0);
                let otherChargesValue = public_toNumber(otherCharges.getValue(),0);

                let totalAmountValue = public_round2(transportCostValue + groundAmountValue + stampDutyValue + insuranceFeeValue + airlinesAddCostValue + otherChargesValue);
                totalAmount.setValue(totalAmountValue);
                if(totalAmountValue==0){
                    masterBillPaymentMethodId.setValue();
                }
            }



            /**
             * 设置控制值，要根据控制类型进行判断，调用不同的方法
             */
            function setObjValue(objId,value,selectType){
                let objType = objId.type;
                /*
                combobox
                textbox
                hidden
                autocomplete
                datepicker
                textarea
                button
                datagrid
                 */
                if(objType=="combobox" || objType == "autocomplete"){
                    if(value.length>4){
                        objId.setValue(value);
                    }
                    else{
                        objId.select(value);
                    }
                }
                else{
                    objId.setValue(value);
                }
            }


            function summaryRight(e){
                e.cellStyle="text-align:right;";
            }



        </script>
    </body>
</html>